# coding:utf-8
from __future__ import absolute_import
from celery import task
from bigscreen.cache_manager import RedisHelper
from bigscreen.models import StationOrders
import json
import re


@task
def add():
    redis_order = RedisHelper().blpop("shell-order")
    if redis_order is None:
        return "无订单信息"
    info = json.loads(redis_order[1])
    print info
    # info = json.loads(
    #     '{"@timestamp":"2017-09-20T09:25:49.354Z","beat":{"hostname":"hadoopmaster","name":"hadoopmaster","version":"5.4.1"},"input_type":"log","message":"2017-09-2017:25:45[com.xjy.paygather.service.order.EventOrderServiceImpl]-[INFO]paySuccess=========推送帐号:shady,推送油站:1075,","offset":701661447,"source":"/home/apache/apache-qiaopai/logs/catalina.out","tags":["tomcat-2"],"type":"shell-order"}')
    # print info['message']
    station_id = re.findall(r":(.+?),", info['message'].split('=========')[1])[1]
    try:
        station_order = StationOrders.objects.get(station_id=station_id)
        station_order.station_num = (station_order.station_num + 1)
        station_order.save()
    except StationOrders.DoesNotExist:
        # StationOrders.objects.create(station_id=station_id, station_num=1)
        print '无该油站信息'
    return "oOK==========="
